package com.shockweb.common.security;


import java.io.UnsupportedEncodingException;

/**
 * sm3工具类
 * @author pengminghua
 *
 */
public class SM3Utils {
	
	

	/**
	 * 生成摘要,二进制
	 * @param data
	 * @return
	 */
	public static byte[] digest(byte[] data){  
    	if(data==null || data.length==0) {
    		return null;
    	}
    	byte[] md = new byte[32];
        SM3Digest sm3 = new SM3Digest();  
        sm3.update(data, 0, data.length);  
        sm3.doFinal(md, 0);
        return md; 
    }


	/**
	 * 生成摘要,字符串输出二进制base64
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String digest(String data,String charset) throws SecurityException{  
		try {
			return Utils.base64EncodeString(digest(data.getBytes(charset)));
        }catch(Exception e) {
        	throw new SecurityException("digest异常",e);
        }
    } 

}